package cn.nexd.app.collector.presenter.setting;


import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.net.wifi.WifiManager;

import com.mylhyl.acp.Acp;
import com.mylhyl.acp.AcpListener;
import com.mylhyl.acp.AcpOptions;

import java.util.List;

import cn.nexd.app.collector.application.NexdApplication;
import cn.nexd.app.collector.model.CollectConfig;
import cn.nexd.utils.framework.tools.Logger;


/**
 * Created by xun on 2016/8/11.
 */
public class SettingPresenter implements SettingContract.Presenter {
    private SettingContract.View settingView;
    private Acp acp;
    private Context mContext;
    private static final String TAG = "SettingPresenter";

    public static final int CHECK_BLUETOOTH_STATE = 0X00FF;
    public static final int CHECK_WIFI_STATE = 0X0000;

    public SettingPresenter(SettingContract.View settingView) {
        this.settingView = settingView;
        this.acp = Acp.getInstance((Context) settingView);
        this.mContext = (Context) settingView;

    }


    @Override
    public void init() {

    }

    @Override
    public void submitMsg(List<CollectConfig> collectConfigs) {
        if (collectConfigs != null && collectConfigs.size() > 0) {
            NexdApplication.getInstance().getCollectConfigs().clear();
            NexdApplication.getInstance().getCollectConfigs().addAll(collectConfigs);
            settingView.printLog(collectConfigs.toString());
            settingView.startActivity();
        } else {
            settingView.showToast("必须填写用户配置");
        }
    }


    @Override
    public void openCollectorSwitch(int checkType) {
        switch (checkType) {
            case CHECK_BLUETOOTH_STATE:
                BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
                if (!adapter.isEnabled()) {
                    adapter.enable();
                }
                break;

            case CHECK_WIFI_STATE:
                WifiManager manager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
                if (!manager.isWifiEnabled()) {
                    manager.setWifiEnabled(true);
                }
                break;

            default:
                Logger.debug(TAG, "这TM是傻逼!");
                break;
        }
    }


    @Override
    public void checkPermission(final int type, String... permission) {
        AcpOptions.Builder builder = new AcpOptions.Builder();

        builder.setPermissions(permission).setDeniedMessage("您拒绝定位信息/存储空间权限将不能正常使用，请您到设置界面重新授权");
        acp.request(builder.build(), new AcpListener() {
            @Override
            public void onGranted() {
                if (type == 0) {
                    settingView.submitSetting();
                } else if (type == 1) {
                    settingView.fileManager();
                }
            }

            @Override
            public void onDenied(List<String> permissions) {
                settingView.showToast(permissions.toString() + "被拒绝");
            }
        });
    }


}



